<template>
  <div class="about-master-container">
    <base-breadcrumb :breadcrumb="!isAudit">
      <b-steps
        :current="stepVal"
        direction="vertical"
        @change.self="stepChange"
      >
        <b-step title="基本信息">
          <a-card
            v-show="activeKey[0]"
            slot="description"
            v-loading="loading"
            :bordered="false"
          >
            <a-form-model
              ref="ruleForm"
              :model="formData"
              :rules="rules"
            >
              <a-row>
                <a-col span="8">
                  <!-- 编辑时才显示-->
                  <a-form-model-item
                    label="物资招标文件评审编码"
                    prop="fileReviewCode"
                  >
                    <a-input-search
                      ref="codeInput"
                      v-model="formData.fileReviewCode"
                      :disabled="disabled"
                      placeholder="请选择"
                      @click="onSearch"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="招标文件编号">
                    <BaseInput
                      v-model="formData.fileReviewCode"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="物资招标评审会签编码">
                    <BaseInput
                      v-model="formData.countersignCode"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="工程名称">
                    <a-select
                      v-model="formData.wbsId"
                      disabled
                    >
                      <a-select-option
                        v-for="item in getProgrammeArr"
                        :key="item.id"
                        :value="item.id"
                      >
                        {{ item.name }}
                      </a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="开标地点"
                    prop="bidOpenAddr"
                  >
                    <BaseInput
                      v-model="formData.bidOpenAddr"
                      :disabled="disabled"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="开标日期"
                    prop="bidOpenDate"
                  >
                    <a-date-picker
                      v-model="formData.bidOpenDate"
                      value-format="YYYY-MM-DD"
                      :disabled="disabled"
                      type="date"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item
                    label="邀请投标单位数量"
                    prop="inviteBidQuantity"
                  >
                    <BaseInputNumber
                      v-model="formData.inviteBidQuantity"
                      :min="0"
                      :disabled="disabled"
                      type="number"
                    />
                  </a-form-model-item>
                </a-col>
                <!--                                <a-col span="8">-->
                <!--                                    <a-form-model-item label="截止时间" prop="deadline">-->
                <!--                                        <a-date-picker :disabled="disabled" v-model="formData.deadline"-->
                <!--                                                       :disabled-date="disabledDate"-->
                <!--                                                       type="date" />-->
                <!--                                    </a-form-model-item>-->
                <!--                                </a-col>-->
                <a-col span="8">
                  <a-form-model-item
                    label="实际投标单位数量"
                    prop="actualBidQuantity"
                  >
                    <BaseInputNumber
                      v-model="formData.actualBidQuantity"
                      :min="0"
                      :disabled="disabled"
                      type="number"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="创建人">
                    <BaseInput
                      v-model="formData.creatorName"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
                <a-col span="8">
                  <a-form-model-item label="创建时间">
                    <BaseInput
                      v-model="formData.createTime"
                      disabled
                    />
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-form-model>
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                v-show="!disabled"
                type="primary"
                @click="temporaryStorage()"
              >
                保存草稿
              </a-button>
              <a-button
                type="primary"
                @click="temporaryStorage(true)"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="投标明细">
          <a-card
            v-if="activeKey[1]"
            slot="description"
            :bordered="false"
          >
            <div class="tender-container">
              <div class="tender-btn pub-detail-title-btn">
                <a-button @click="exportSelect">
                  导出
                </a-button>
                <a-radio-group v-model="tabVal">
                  <a-radio-button :value="0">
                    物资维度
                  </a-radio-button>
                  <a-radio-button :value="1">
                    供应商维度
                  </a-radio-button>
                </a-radio-group>
              </div>
            </div>
            <materials
              v-if="!tabVal"
              :id="row ? row.id : null"
              ref="materials"
              :state="state"
            />
            <supplier
              v-else
              :id="row ? row.id : null"
              ref="supplier"
              :state="state"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
              <a-button
                type="primary"
                @click="next"
              >
                下一步
              </a-button>
            </div>
          </a-card>
        </b-step>
        <b-step title="相关附件">
          <a-card
            v-if="activeKey[2]"
            slot="description"
            :bordered="false"
          >
            <u-table
              :is-show="!disabled"
              :upload-list="addFile"
              :delete-url="fileDel"
              :list-url="fileList"
              :business-id="row.id"
              business-code="MM05"
            />
            <div
              v-if="!isAudit"
              class="pub-next-btn"
            >
              <a-button
                type="primary"
                @click="previous"
              >
                上一步
              </a-button>
            </div>
          </a-card>
        </b-step>
      </b-steps>
      <review-list
        ref="reviewList"
        @getRow="getRow"
      />
    </base-breadcrumb>
    <div
      v-if="!isAudit"
      class="pub-header-btn page-btn-right-top"
    >
      <a-button
        v-if="state !== 3"
        :loading="loading"
        type="primary"
        @click="submit"
      >
        提交
      </a-button>
      <BasePrintButton
        v-if="formData.id && state === 3"
        :before-print-fn="beforePrintFn"
        style="display: inline-block"
      />
      <a-button
        v-checkVisibleInDetail
        @click="$close"
      >
        关闭
      </a-button>
    </div>
  </div>
</template>

<script>
import reviewList from '../components/reviewList/reviewList'
import {
    signAddBasic, signEditBasic, audit,
    addFile, fileList, fileDel, signBasicDetail,print
} from '@/api/material/procurement/sign'
import materials from '../components/materials/materials'
import supplier from '../components/supplier/supplier'
import { selectOption } from '@/api/material/goods/goods'
import moment from 'moment'


export default {
	name:'a'+Date.now(),
    components: {
        materials,
        reviewList,
        supplier
    },
    props: {
        isAudit: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            beforePrintFn: async () => {
                let res = await print({ id: this.formData.id })
                return res.data.content
            },
            activeKey: [true, false, false, false, false, false, false],
            state: Number(this.$route.query.state),
            isSubmit: false,
            loading: false,
            row: this.$route.query.row,
            formData: {
                createTime: this.getNowTime(),
                fileReviewId: null,
                fileReviewCode: null,
                creatorName: JSON.parse(sessionStorage.getItem('ycloud-user_info')).name,
                countersignCode: '',
                wbsId: null,
                bidOpenAddr: '',
                bidOpenDate: '',
                inviteBidQuantity: null,
                deadline: '',
                actualBidQuantity: ''
            },
            tabVal: 0,
            stepVal: 0,
            isNext: false,
            rules: {
                fileReviewCode: { required: true, message: '请选择物资招标文件评审编码', trigger: 'change' },
                bidOpenAddr: { required: true, message: '请输入开标地点', trigger: 'blur' },
                bidOpenDate: { required: true, message: '请选择开标日期', trigger: 'change' },
                inviteBidQuantity: { required: true, message: '请输入邀请投标单位数量', trigger: 'blur' },
                deadline: { required: true, message: '请选择截止时间', trigger: 'change' },
                actualBidQuantity: { required: true, message: '请输入实际投标单位数量', trigger: 'blur' }
            },
            addFile: addFile(),
            fileDel: fileDel(),
            fileList: fileList(),
            getProgrammeArr: []
        }
    },
    mounted: function() {
        this.$nextTick(() => {
            if (this.state !== 1) {
                this.getEditInfo()
                this.isSubmit = true
            }
        })
        this.getProgramme()
    },
    watch: {
        'formData.bidOpenDate'(newVal, oldVal) {
            if (this.formData.deadline) {
                if (this.formData.deadline < newVal) {
                    this.formData.deadline = null
                }
            }
        },
        isAudit: {
            immediate: true,
            handler(val) {
                if (val) {
                    this.state = 3
                }
            }
        },
        stepVal(newVal, oldVal) {
            this.$set(this.activeKey, newVal, true)
        }

    },
    computed: {
        disabled() {
            return this.state === 3
        }
    },
    methods: {
        exportSelect() {
            if (this.tabVal) {
                this.$bus.$emit('exportSelectSupplier')
            } else {
                this.$bus.$emit('exportSelectMaterials')
            }
        },
        moment,
        disabledDate(current) {
            if (this.formData.bidOpenDate) {
                return current && current < this.formData.bidOpenDate.endOf('day')
            }
            return true
        },
        previous: function() {
            this.isNext = true
            this.stepVal--
            if (this.stepVal < 0) {
                this.stepVal = 0
            }
        },
        getProgramme: function() {
            selectOption({
                type: 18
            }).then(res => {
                if (res.code === 200) {
                    this.getProgrammeArr = res.data
                }
            })
        },
        next: function(val) {
            this.isNext = true
            this.stepVal++
        },
        stepChange: function(val) {
            if (!this.row && !this.isAudit) {
                if (val) {
                    this.$message.warning('切换步骤前请先填写基本信息')
                    this.stepVal = 0
                }
            } else {
                if (!this.isNext) {
                    this.$set(this.activeKey, val, !this.activeKey[val])
                }
            }
            this.isNext = false
        },
        onSearch: function(val) {
          this.$refs.codeInput.blur()
          this.$refs.reviewList.open()
        },
        getRow: function(val) {
            console.log(val)
            this.formData.fileReviewId = val.id
            this.formData.fileReviewCode = val.fileCode
            this.formData.wbsId = val.projectId
        },
        // 获取编辑回显数据
        async getEditInfo() {
            const params = {
                id: this.$route.query.id || this.row.id
            }
            this.loading = true
            const res = await signBasicDetail(params)
            if (res.code === 200) {
                this.row = res.data || {}
                this.formData.id = res.data.id
                this.formData.fileReviewId = this.row.fileReviewId
                this.formData.fileReviewCode = this.row.fileReviewCode
                this.formData.fileNumber = this.row.fileNumber
                this.formData.createTime = this.row.createTime
                this.formData.creatorName = this.row.creatorName
                this.formData.countersignCode = this.row.countersignCode
                this.formData.wbsId = this.row.wbsId
                this.formData.bidOpenAddr = this.row.bidOpenAddr
                this.formData.bidOpenDate = this.row.bidOpenDate
                this.formData.inviteBidQuantity = this.row.inviteBidQuantity
                this.formData.deadline = this.row.deadline
                this.formData.actualBidQuantity = this.row.actualBidQuantity
            }
            this.loading = false
        },
        submit() {
            // this.save('approve');
            if (this.isSubmit) {
                this.$submitConfirm(async () => {
                    this.loading = true
                    let res = await audit({ baseId: this.row.id, auditStatus: this.row.auditStatus })
                    this.loading = false
                    if (res.code === this.SUCCESS_CODE){
                        this.loading = false
                        this.$message.success('提交成功')
                        this.$router.push({ path: '/material/procurement/sign' })
                    }
                })
            } else {
                this.$message.warning('提交之前请先暂存')
            }
        },
        temporaryStorage(isNext) {
            if (this.state === 3) {
                this.next()
                return
            }
            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    this.loading = true
                    this.formData.bidOpenDate = this.getNowTime(this.formData.bidOpenDate)
                    if (this.state === 2 || this.row) {
                        this.formData.id = this.row.id
                        signEditBasic(this.formData).then(res => {
                            if (res.data.code === 200) {
                                this.$message.success('暂存成功')
                                this.isSubmit = true
                                if (isNext) {
                                    this.next()
                                }
                            }
                            this.loading = false
                        })
                    } else {
                        signAddBasic(this.formData).then(res => {
                            if (res.code === 200) {
                                this.row = res.data
                                this.$message.success('暂存成功')
                                this.isSubmit = true
                                this.getEditInfo()
                                if (isNext) {
                                    this.next()
                                }
                            }
                            this.loading = false
                        })
                    }
                } else {
                  this.$message.warning("请填写完所有必填项信息")
                    return false
                }
            })
        }
    }
}
</script>
<style scoped>
.tender-container .ant-radio-group {
    float: right;
}
</style>
